import QtQuick 2.3
import QtQuick.Controls 1.2

ApplicationWindow {
    visible: true
    width: 640
    height: 480
    title: qsTr("DynamicListModel")
    id: root

    Component {
        id: listDelegate
        Text {
            id: wrapper;
            width: root.width;
            height: 32;
            font.pointSize: 15;

            verticalAlignment: Text.AlignVCenter;
            horizontalAlignment: Text.AlignHCenter;
            text: content;
            color: ListView.view.currentIndex === index?"red":"blue";
            MouseArea {
                anchors.fill: parent;
                onClicked: {
                    if(wrapper.ListView.view.currentIndex !== index) {
                        wrapper.ListView.view.currentIndex = index;
                    }
                }
            }
        }
    }

    ListView {
        id: dynamicList;
        z: 1;
        anchors.fill: parent;
        anchors.margins: 10;
        delegate: listDelegate;
        model: dynamicModel;

        focus: true;
        activeFocusOnTab: true;
        highlight: Rectangle {
            color: "steelblue";
        }
        property real contentYOnFlickStarted: 0;
        onFlickStarted: {
            contentYOnFlickStarted = contentY;
        }
        onFlickEnded: {
            dynamicModel.loadMore(contentY < contentYOnFlickStarted);
        }
    }
}

